<script setup lang="ts">
import { useRouter } from 'vue-router'
import { useUserStore } from '@store/user'
import { storeToRefs } from 'pinia'

const router = useRouter()
const { existGetter } = storeToRefs(useUserStore())
defineOptions({
  name: 'MainHeader',
})

const props = defineProps({
  isFull: {
    type: Boolean,
    default: false,
  },
})
const emit = defineEmits(['fullChange'])

const handleHomeClick = () => {
  router.push('/')
}
</script>

<template>
  <div class="main-header-component">
    <div class="unfold c-center" @click="emit('fullChange')">
      <ElIcon size="20" v-if="props.isFull">
        <Expand />
      </ElIcon>
      <ElIcon size="20" v-else>
        <Fold />
      </ElIcon>
    </div>
    <div class="unfold c-center" @click="handleHomeClick">
      <ElIcon size="20"><HomeFilled /></ElIcon>
    </div>
    <div class="main-header-com-tabs"></div>
    <div class="main-header-com-right">
      <div class="right-atom" v-if="existGetter">
        <!-- 暂时注释，避免遮挡顶部跳转loading动画 -->
        <!-- <div class="atomSpin c-center">
          <SvgIcon name="atom" size="1.5em"></SvgIcon>
        </div> -->
      </div>
      <div class="right-login-and-register" v-else>
        <ElText type="primary" @click="$router.push('/login')"> 登陆 </ElText>
        <span class="division">|</span>
        <ElText type="success" @click="$router.push('/register')">
          注册
        </ElText>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.main-header-component {
  width: 100%;
  height: 50px;
  padding: 0 8px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid var(--lineColor);
  background-color: var(--backColor-simple);

  .unfold {
    width: 50px;
    height: 35px;
    border-radius: 3px;
    cursor: pointer;
    color: var(--textGrayColor);
    background-color: var(--defaultBack);
    margin-right: 6px;
    transition:
      color 0.3s,
      background-color 0.3s;

    &:hover {
      color: var(--textColor);
      background-color: var(--primaryBack);
    }
  }

  .main-header-com-tabs {
    flex: 1;
    height: 1px;
  }

  .main-header-com-right {
    width: 120px;
    height: 100%;

    .right-login-and-register {
      width: 100%;
      height: 100%;
      display: flex;
      justify-content: flex-end;
      align-items: center;

      .el-text {
        cursor: pointer;
        opacity: 0.7;
        transition: opacity 0.3s;

        &:hover {
          opacity: 1;
        }
      }

      .division {
        margin: 0 8px;
        color: var(--lineColor);
        user-select: none;
      }
    }

    .right-atom {
      width: 100%;
      height: 100%;
      display: flex;
      justify-content: flex-end;
      align-items: center;

      .atomSpin {
        width: 30px;
        height: 30px;
        border-radius: 50%;
        background-color: var(--backColor);
        animation: avatarAtomSpinAnimation 10s linear infinite;
      }
    }
  }
}

@keyframes avatarAtomSpinAnimation {
  0% {
    transform: rotate(0deg);
  }

  100% {
    transform: rotate(360deg);
  }
}
</style>
